import logging

import datetime

from flask import Flask,request,jsonify
from flask_cors import CORS
from views.comany import bp_comany
from views.zuzhi_bp import bp_zuzhi
from views.login_bp import bp_login
from utils.tokens import token1

from utils.acl import acl

# app = Flask(__name__)

def create_app():
    app = Flask(__name__)
    with app.app_context():
        pass
    return app
app = create_app()

app.register_blueprint(bp_comany)
app.register_blueprint(bp_zuzhi)
app.register_blueprint(bp_login)


CORS(app)

@app.route('/')
def hello_world():
    return 'Hello World!'


@app.before_request
def before_request():
    white =  ['/mylogin']
    try:
        token = request.headers.get("Token")
    except:
        token = None
    if token:
        user = token1.decode_jwt(token,"123")
        if user:
            reslist = acl.get_page_reslist(user['id'])
            for i in reslist:
                white.append(i)
            if request.path not in white:
                return jsonify({"code": 403, "msg": "fail"})
            else:
                pass
        else:
            jsonify({'code':403,'msg':'您的token已经被别人篡改'})
    else:
        return jsonify({"code": 40311, "msg": "未获取到该token"})








if __name__ == '__main__':
    filename = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d')
    handler = logging.FileHandler("logs/" + filename + '.txt',encoding="utf8")
    app.logger.addHandler(handler)
    app.run(port=8000)
